package com.hadwinling.blog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hadwinling.blog.entity.Admin;
import com.hadwinling.blog.entity.Money;
import com.hadwinling.blog.entity.Moneytag;
import com.hadwinling.blog.service.AdminService;
import com.hadwinling.blog.service.MoneyService;
import com.hadwinling.blog.service.MoneytagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author HadwinLing
 * @version V1.0
 * @Package com.hadwinling.blog.controller
 * @date 2020/8/29 16:19
 */
@Controller
public class UserMoneyController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private MoneyService moneyService;
    @Autowired
    private MoneytagService moneytagService;

    @Value("${admin.article.page.pageSize}")
    private int pageSize;
    @Value("${page.navigatePage}")
    private int navigatePage;

    /**
     * 跳转到日常界面
     * @param pageNum
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/user/money")
    public String toMoneyPage(@RequestParam(name = "pageNum",defaultValue = "1") int pageNum,
                              HttpServletRequest request,
                              Model model){
        Admin currUser = (Admin) request.getSession().getAttribute("currUser");
        PageHelper.startPage(pageNum,pageSize);
        List<Money> moneyList = moneyService.selectMoneyByAdminidAndStateOrderByCreatetimeDesc(currUser.getId(), 1);
        PageInfo<Money> pageInfo = new PageInfo<>(moneyList,5);
        //当前页码
        int currPage = pageInfo.getPageNum();
        //总记录
        Long total = pageInfo.getTotal();
        //总页数
        int totalPage = pageInfo.getPages();
        //所有页面
        int [] pages = pageInfo.getNavigatepageNums();
        //保存分页信息
        model.addAttribute("currPage",currPage);
        model.addAttribute("total",total);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("pages",pages);

        model.addAttribute("moneyList",moneyList);
        return "UserCenter/moneyDetail";
    }

    /**
     * 跳转到记账界面
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/user/addMoney")
    public String  toAddMoneyPage(Model model,HttpServletRequest request) {
        List<Moneytag> moneytagList = moneytagService.selectAllByState(1);
        model.addAttribute("moneytagList",moneytagList);
        return "UserCenter/moneyInput";
    }

    /**
     * 执行记账操作
     * @param money
     * @param request
     * @return
     */
    @PostMapping("/user/addMoney")
    public String addMoney(Money money, HttpServletRequest request){
        String MoneyTagId = request.getParameter("moneyTagId");
        money.setTag(Integer.parseInt(MoneyTagId));
        Admin currUser = (Admin) request.getSession().getAttribute("currUser");
        money.setCreatetime(new Date());
        money.setState(1);
        money.setAdminid(currUser.getId());
        moneyService.insertSelective(money);
        return "redirect:/user/money";
    }

    /**
     * 跳转到修改记账界面
     * @param moneyid
     * @param model
     * @return
     */
    @GetMapping("/user/updateMoney/{moneyid}")
    public String toUpdateMoneyPage(@PathVariable(name = "moneyid") int moneyid,Model model){
        Money money = moneyService.selectByPrimaryKey(moneyid);
        model.addAttribute("money",money);
        List<Moneytag> moneytagList = moneytagService.selectAllByState(1);
        model.addAttribute("moneytagList",moneytagList);
        return "UserCenter/moneyUpdate";
    }

    /**
     * 执行修改记账操作
     * @param money
     * @param request
     * @return
     */
    @PostMapping("/user/updateMoney")
    public String updateMoney(Money money, HttpServletRequest request){
        String MoneyTagId = request.getParameter("moneyTagId");
        money.setTag(Integer.parseInt(MoneyTagId));
        Admin currUser = (Admin) request.getSession().getAttribute("currUser");
        money.setCreatetime(new Date());
        money.setState(1);
        money.setAdminid(currUser.getId());
        moneyService.updateByPrimaryKeySelective(money);
        return "redirect:/user/money";
    }

    /**
     * 执行删除日常操作
     * @param moneyid
     * @param request
     * @return
     */
    @PostMapping("/user/deleteMoney")
    @ResponseBody
    public String deleteMoney(@RequestParam(name = "moneyid") int moneyid,HttpServletRequest request){
        Admin currUser = (Admin) request.getSession().getAttribute("currUser");
        moneyService.updateStateByIdAndAdminid(0,moneyid,currUser.getId());
        return "1";
    }





}
